#define _CRT_SECURE_NO_WARNINGS 1

#include <vector>
#include <queue>
using namespace std;
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> vv;
        queue<TreeNode*> q;
        q.push(root);
        int levelsize = root == nullptr ? 0 : 1;
        while (levelsize > 0)
        {
            vector<int> v;
            while (levelsize--)
            {
                TreeNode* tmp = q.front();
                if (tmp->left)
                    q.push(tmp->left);
                if (tmp->right)
                    q.push(tmp->right);
                v.push_back(tmp->val);
                q.pop();
            }
            vv.push_back(v);
            levelsize = q.size();
        }
        return vv;
    }
};